package com.dmgameclient.adapter;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

import com.dmgameclient.R;
import com.dmgameclient.beans.Article;
import com.dmgameclient.common.AdapterImageUtils;
import com.dmgameclient.common.StringUtils;
import com.dmgameclient.common.URLHelper;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @name ArticleListViewAdapter
 * @Descripation <br>
 *               1、<br>
 *               2、<br>
 * @author 樊俊彬
 * @date 2014-9-21
 * @version 1.0
 */
public class ArticleListViewAdapter extends BaseAdapter {

	private static final String TAG = "ArticleListViewAdapter";

	private Context context;
	private List<Article> list;
	private int itemViewResource;
	private AdapterImageUtils adapterImageUtils;

	class ItemHolder {
		public TextView titleText;
		public TextView pubDateText;
		public TextView clickText;
		public ImageView isTodayImageView;
		public ImageView overImageView;
	}

	public ArticleListViewAdapter(Context context, int itemViewResource,
			List<Article> list, View view) {
		this.context = context;
		this.itemViewResource = itemViewResource;
		this.list = list;
		adapterImageUtils = new AdapterImageUtils(context, view,
				R.drawable.default_image, 100, 100);
	}

	public int getCount() {
		return list.size();
	}

	public Object getItem(int arg0) {
		return list.get(arg0);
	}

	public long getItemId(int arg0) {
		return arg0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		ItemHolder itemHolder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					this.itemViewResource, null);
			itemHolder = new ItemHolder();

			itemHolder.titleText = (TextView) convertView
					.findViewById(R.id.titleText);
			itemHolder.pubDateText = (TextView) convertView
					.findViewById(R.id.pubDateText);
			itemHolder.clickText = (TextView) convertView
					.findViewById(R.id.clickText);
			itemHolder.isTodayImageView = (ImageView) convertView
					.findViewById(R.id.isTodayImageView);
			itemHolder.overImageView = (ImageView) convertView
					.findViewById(R.id.overImageView);

			convertView.setTag(itemHolder);

		} else {
			itemHolder = (ItemHolder) convertView.getTag();
		}
		// 设置文本内容和图片
		Article article = list.get(position);
		itemHolder.titleText.setText(article.getTitle());
		Date date = new Date(Long.valueOf(article.getPubdate()+"000"));
		String dateString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
		itemHolder.pubDateText.setText(dateString);
		itemHolder.clickText.setText(article.getFeedback());

		String url = URLHelper.BASE_URL + article.getLitpic();
		String fileName = url.substring(url.lastIndexOf("/") + 1);
		String folder = URLHelper.CACHE_IMAGE_ARTICLE;
		article.setLitpicFilename(fileName);

		// Log.i(TAG, ">>>图片信息>>" + url + "," + fileName + "," + folder);

		adapterImageUtils.executeImageView(itemHolder.overImageView,
				R.drawable.ic_launcher, url, folder, fileName);
		return convertView;
	}
}